#include "drv_led.h"
#include "drv_beep.h"
#include "drv_systick.h"
#include "drv_key.h"
#include "drv_exti.h"
#include "drv_fan.h"
#include "drv_usart.h"
#include "drv_motor.h"
#include "drv_timer.h"
#include <stdio.h>
#include "FreeRTOS.h"
#include <task.h>
#include <queue.h>
#include "drv_wifi.h"
#include "drv_sht20.h"
#include "drv_74hc595.h"
#define START_TASK_TACK 128
#define START_TASK_PRIORITY 0
TaskHandle_t start_task_handle;

#define KEY_TACK 128
#define KEY_PRIORITY 0
TaskHandle_t key_handle;

#define LED_TACK 128
#define LED_PRIORITY 1
TaskHandle_t led_handle;

uint8_t led_flg=0;
QueueHandle_t key_queue;

void drv_key_task(void * pvParameters)
{
	printf("1\r\n");
	for(;;)
    {
        printf("%s\r\n", __FUNCTION__);
		if(GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_9)==RESET)
		{
			vTaskDelay(20);
			if(GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_9)==RESET)
			{
				while(GPIO_ReadInputDataBit(GPIOF, GPIO_Pin_9)==RESET);
				led_flg = ~led_flg;
				xQueueSend(key_queue, &led_flg, portMAX_DELAY);
			}
		}
        vTaskDelay(100);  
    }
}

void drv_led_task(void * pvParameters)
{
	uint8_t value;
	printf("2\r\n");
	for(;;)
    {
        printf("%s\r\n", __FUNCTION__);
		xQueueReceive(key_queue, &value, portMAX_DELAY);
		if(value)
		{
			drv_beep_on();
		}
		else
		{
			drv_beep_off();
		}
        vTaskDelay(100);  
    }
}

void drv_start_task( void * pvParameters )
{
	BaseType_t ret;
	printf("%s\r\n",__FUNCTION__);
	taskENTER_CRITICAL();//进入临界保护
	key_queue = xQueueCreate(1, sizeof(uint8_t));
	if(key_queue == NULL)
	{
		printf("xQueueCreate err\r\n");
	}
	else
	{
		printf("xQueueCreate ok\r\n");
	}
	ret = xTaskCreate(drv_key_task, "drv_key_task", KEY_TACK, NULL, KEY_PRIORITY, &key_handle);
	if(ret != pdPASS)
	{
		printf("task_1 creat fail!\r\n");
	}
	ret = xTaskCreate(drv_led_task, "drv_led_task", LED_TACK, NULL, LED_PRIORITY, &led_handle);
	if(ret != pdPASS)
	{
		printf("task_2 creat fail!\r\n");
	}
	vTaskDelete(start_task_handle);
	taskEXIT_CRITICAL();//退出临界保护
}

uint8_t key_flg1=0;
uint8_t key_flg2=0;
uint8_t key_flg3=0;
uint8_t key_flg4=0;
uint8_t key_flg5=1;
uint8_t key_flg6=1;
uint8_t wifi_led_flg=0;
uint8_t v_beep_flg=0;//蜂鸣器
uint8_t v_fan_flg=0;//风扇
uint8_t v_motor_flg=0;//马达


int main(void)
{
	BaseType_t ret;
	//sht20_val data = {0};
	float data;
	uint16_t val;
	uint8_t i,a,b,c,d;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	drv_led_init();
	drv_beep_init();
	drv_fan_init();
	drv_motor_init();
	drv_systick_init(168);
	drv_key_init();
	drv_extif9_init();
	drv_extic_init();
	drv_usart3_init();
	drv_usart6_init();
	drv_usart2_init();
	//drv_wifi_init();
	drv_sht20_init();
	drv_74hc595_init();
	drv_timer3_init();
	drv_timer4_init();
	//drv_timer6_init();
//	ret = xTaskCreate(drv_start_task, "drv_start_task", START_TASK_TACK, NULL, START_TASK_PRIORITY, &start_task_handle);
//	if(ret != pdPASS)
//	{
//		printf("start_task creat fail!\r\n");
//	}

//	vTaskStartScheduler();
	//printf("hello 2505 class!\r\n");

	while(1)
	{
		if(wifi_led_flg)drv_led_open();else{drv_led_close();}
		if(v_beep_flg)drv_beep_on();else{drv_beep_off();}
		if(v_fan_flg)drv_fan_on();else{drv_fan_off();}
		if(v_motor_flg)drv_motor_on();else{drv_motor_off();}

		//if(key_flg5 == 0)
        //{
			i=20;
			if(key_flg6 == 0)
			{
				data = drv_sht20_getdata();
				val = (uint16_t)(data*100);
				//val = (uint16_t)(data.hum_RH*100);
				a = val / 1000;      // 千位（十位数）
				b = (val / 100) % 10;  // 百位（个位数）
				c = (val / 10) % 10;   // 十位（小数点后第一位）
				d = val % 10;        // 个位（小数点后第二位）
			}
			//else
			//{
			//	data = drv_sht20_getdata();
			//	val = (uint16_t)(data.hum_T*100);
			//	a = val / 1000;      // 千位（十位数）
			//	b = (val / 100) % 10;  // 百位（个位数）
			//	c = (val / 10) % 10;   // 十位（小数点后第一位）
			//	d = val % 10;        // 个位（小数点后第二位）
			//}
		//}
	drv_seg_display(1, 10);
	drv_seg_display(0, a);
	drv_seg_display(1, b);
	drv_seg_display(2, c);
	drv_seg_display(3, d);

	}
}

